﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

public partial class Delivery_DeliveryAllList : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();
    DeliveryModel model = new DeliveryModel();
    DeliveryDAO comm = new DeliveryDAO();
    DeliveryListBuild build = new DeliveryListBuild();
    private string chkFlag = "block";
    public static int insertflag = 0;

    #region
    private string _tradingTable;
    public string tradingTable
    {
        get { return _tradingTable; }
        set { _tradingTable = value; }
    }
    public string getTrading()
    {
        return tradingTable;
    }
    #endregion
    #region
    private string _userCode;
    public string userCode
    {
        get { return _userCode; }
        set { _userCode = value; }
    }
    public string getUserCode()
    {
        return userCode;
    }
    #endregion
    #region
    private string _coCode;
    public string coCode
    {
        get { return _coCode; }
        set { _coCode = value; }
    }
    public string getCoCode()
    {
        return coCode;
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        userCode = "'" + Session["UserCode"].ToString() + "'";
        coCode = "'" + Session["CoCode"].ToString() + "'";
        if (!IsPostBack)
        {
            // 页面权限
            comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "DeliveryAllList" }, null, new string[] { }, new int[] { }, CommonModel.loginOut1);
            Initi();
        }
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    public void Initi()
    {
        model.UserCode = Session["UserCode"].ToString();
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = "";// TradingTableBuild(model);

        DdlBind();
    }
    public void GetHead()
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        string sql = string.Format("select  distinct RC.ColumnPosition, RC.ColumnType, RC.ColumnName, RC.ColumnWidth, "
            + "RC.BelongToTableName, RC.ColumnDesc,RC.RepeatFlag from B_Roles_Column RC "
            + " join M_UserDefineShowColumn UDC on Patindex('%'+RC.ColumnType+'%',UDC.ColumnTypes)<1 and UDC.UserCode='{0}' "
            + "where	RC.BelongToTableName='M_SO_Trading' order by ColumnPosition", Session["UserCode"].ToString());

        Session["headDt"] = help.GetDataTable(sql);
    }
    public DataTable QueryBind(int n)
    {
        try
        {
            StringBuilder sql = new StringBuilder();
            if (n == 1)
            {
                if (!string.IsNullOrEmpty(txtContractNo.Text))
                {
                    sql.Append(string.Format(" and TDAL.ContractNoMain like '%{0}%' ", txtContractNo.Text.Trim()));
                }
                if (!string.IsNullOrEmpty(txtContractNoCustomer.Text))
                {
                    sql.Append(string.Format(" and TDA.ContractNoCust like '%{0}%' ", txtContractNoCustomer.Text.Trim()));
                }
                if (!string.IsNullOrEmpty(txtSoNo.Text))
                {
                    sql.Append(string.Format(" and TDAL.TradingSoNo like '%{0}%' ", txtSoNo.Text.Trim()));
                }
                if (!string.IsNullOrEmpty(txtSaleName.Text))
                {
                    sql.Append(string.Format(" and TDAL.ContractNoMain in (select distinct ContractNoMain from M_SalesCatalog where SalesName like '%{0}%' )",
                        txtSaleName.Text.Trim()));
                }
                if (!string.IsNullOrEmpty(txtCustomer.Text))
                {
                    sql.Append(string.Format(" and TDAL.ContractNoMain in (select distinct ContractNoMain from M_Customer where CustomerName like '%{0}%' )",
                        txtCustomer.Text.Trim()));
                }
                if (!ddlState.SelectedValue.Equals("-1"))
                {
                    if (ddlState.SelectedValue.Equals("NotDeliveried"))
                    { 
                        sql.Append(" and cast(isnull(TradingSelectedQTYForShippment,0) as float)-cast(isnull(TradingShippedQty,0) as float)>0 ");
                        sql.Append(" and cast(TradingOrderQty as float)<>cast(TradingShippedQty as float) ");
                        sql.Append(" and Lower(TDAL.TradingState)<>'return' ");
                    }
                    else
                    { sql.Append(string.Format(" and TDAL.TradingState='{0}' ", ddlState.SelectedValue)); }
                }
                if (!string.IsNullOrWhiteSpace(txtTradingSupplierOA.Text))
                {
                    sql.Append(string.Format(" and TradingSupplierOA like '%{0}%'", txtTradingSupplierOA.Text.Trim()));
                }
                if (!string.IsNullOrWhiteSpace(txtDeliveryApplyNo.Text))
                {
                    sql.Append(string.Format(" and TDAL.DeliveryApplyNo like '%{0}%'", txtDeliveryApplyNo.Text.Trim()));
                }
                if (!string.IsNullOrWhiteSpace(txtFileName.Text))
                {
                    sql.Append(string.Format(" and TDA.DeliveryApplyfileName like '%{0}%'", txtFileName.Text.Trim()));
                }
                if (!string.IsNullOrWhiteSpace(txtRemark.Text))
                { sql.Append(string.Format(" and TDA.Remark like '%{0}%'", txtRemark.Text.Trim())); }
            }
            if (hidinsertflag.Value.Equals("0"))
            {
                help.RunProc("tempT_Trading_Plant_proc");
                hidinsertflag.Value = "1";
            }
            return help.RunDataTableProc("GetDeliveryAllList", new SqlParameter[] { new SqlParameter("@Condiction", sql.ToString()) });
        }
        catch { return null; }
    }

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        HidQueryFlag.Value = "1";

        model.UserCode = Session["UserCode"].ToString();
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    // 重置
    protected void cmdReset_Click(object sender, EventArgs e)
    {
        txtDateFrom.Value = "";
        txtDateTo.Value = "";
        txtContractNo.Text = "";
        txtCustomer.Text = "";
        txtSaleName.Text = "";
        txtSoNo.Text = "";
        HidQueryFlag.Value = "0";
        ddlState.SelectedIndex = 0;
        this.lblTotal.Text = "0";
    }

    public DataTable getTablesForBuild()
    {
        DataTable dt = null;
        if (Session["rowDt"] != null)
        {
            QueryBind(Convert.ToInt16(HidQueryFlag.Value));
        }
        dt = (DataTable)Session["rowDt"];
        return dt;
    }
    public string TradingTableBuild(DeliveryModel model)
    {
        DataTable dt = QueryBind(Convert.ToInt16(HidQueryFlag.Value));
        if (dt !=null && dt.Rows.Count > 0)
        {
            StringBuilder table = new StringBuilder();
            table.Append("<table class=\"deliveryApplytTable\" id=\"contractTB\"  border=\"1px\" cellpadding=\"0px\" cellspacing=\"0px\"><thead>");
            table.Append(build.TradingHeadBuild(model));
            table.Append("</thead>");
            table.Append(build.TradingRowBuild(dt, model));
            table.Append("</table>");

            SetPage(dt, model);
            Session["ApplyList"] = table.ToString();
            return table.ToString();
        }
        return "Can not get data！";
    }
    // 换页设置
    public void SetPage(DataTable dt, DeliveryModel model)
    {
        int toaltNum = dt.Rows.Count;
        int pagesize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        this.lblTotal.Text = toaltNum.ToString();
        int pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        int pageCount = toaltNum / pagesize;

        if (pageCount * pagesize == toaltNum) { }
        else { pageCount = pageCount + 1; }

        txtNewPageIndex.Value = pageIndex.ToString();

        this.lblPageCount.Text = pageCount.ToString();

        if (pageIndex < pageCount)
        {
            if (pageIndex == 1)
            {
                lbtFirstPage.Enabled = false;
                lbtPreviousPage.Enabled = false;
                lbtNextPage.Enabled = true;
                lbtLastPage.Enabled = true;
            }
            else
            {
                lbtFirstPage.Enabled = true;
                lbtPreviousPage.Enabled = true;
                lbtNextPage.Enabled = true;
                lbtLastPage.Enabled = true;
            }
        }
        else
        {
            if (pageIndex > 1)
            {
                lbtFirstPage.Enabled = true;
                lbtPreviousPage.Enabled = true;
                lbtNextPage.Enabled = false;
                lbtLastPage.Enabled = false;
                btnGo.Enabled = true;
            }
            else
            {
                lbtFirstPage.Enabled = false;
                lbtPreviousPage.Enabled = false;
                lbtNextPage.Enabled = false;
                lbtLastPage.Enabled = false;
                btnGo.Enabled = false;
            }
        }
    }

    #region 换页
    // 跳页
    protected void btnGo_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        model.UserCode = Session["UserCode"].ToString();
        string temp = tradingTable;
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(txtNewPageIndex.Value.ToString());
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        if (!(model.pageIndex == model.pageNextIndex))
        {
            model.chkFlag = chkFlag;
            temp = TradingTableBuild(model);
        }
        tradingTable = temp;
    }
    //第一页
    protected void lbtFirstPage_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        model.UserCode = Session["UserCode"].ToString();
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);
    }
    // 上一页
    protected void lbtPreviousPage_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        model.UserCode = Session["UserCode"].ToString();
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(lblPageIndex.Text.ToString()) - 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);
    }
    // 下一页
    protected void lbtNextPage_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        model.UserCode = Session["UserCode"].ToString();
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(lblPageIndex.Text.ToString()) + 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);

    }
    // 最后一页
    protected void lbtLastPage_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        model.UserCode = Session["UserCode"].ToString();
        model.pageIndex = Convert.ToInt16(lblPageCount.Text.ToString()) - 1;
        model.pageNextIndex = Convert.ToInt16(lblPageCount.Text.ToString());
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        model.UserCode = Session["UserCode"].ToString();
        tradingTable = TradingTableBuild(model);
    }
    #endregion

    public void ChangeBind(object sender, EventArgs e)
    {
        if (Session["ApplyList"] != null)
        { tradingTable = Session["ApplyList"].ToString(); }
        else { cmdSearch_Click(sender, e); }
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    public void DdlBind()
    {
        ListItem item = new ListItem("请选择(Select Please)", "-1");
        DataTable[] dt = comm.GetddlForApplyListQuery();
        ddlContractNo.DataSource = dt[0];
        ddlContractNo.DataValueField = "ContractNoMain";
        ddlContractNo.DataTextField = "ContractNoMain";
        ddlContractNo.DataBind();
        ddlContractNo.Items.Insert(0, item);
        txtContractNo.Text = "";

        ddlContractNoCustomer.DataSource = dt[6];
        ddlContractNoCustomer.DataValueField = "ContractNoCust";
        ddlContractNoCustomer.DataTextField = "ContractNoCust";
        ddlContractNoCustomer.DataBind();
        ddlContractNoCustomer.Items.Insert(0, item);
        txtContractNoCustomer.Text = "";

        ddlSoNo.DataSource = dt[1];
        ddlSoNo.DataValueField = "ContractNoMain";
        ddlSoNo.DataTextField = "TradingSoNo";
        ddlSoNo.DataBind();
        ddlSoNo.Items.Insert(0, item);
        txtSoNo.Text = "";

        ddlSaleName.DataSource = dt[2];
        ddlSaleName.DataValueField = "SalesName";
        ddlSaleName.DataTextField = "SalesName";
        ddlSaleName.DataBind();
        ddlSaleName.Items.Insert(0, item);
        txtSaleName.Text = "";

        ddlCustomer.DataSource = dt[3];
        ddlCustomer.DataValueField = "CustomerName";
        ddlCustomer.DataTextField = "CustomerName";
        ddlCustomer.DataBind();
        ddlCustomer.Items.Insert(0, item);
        txtCustomer.Text = "";
    }
    protected void ddlContractNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        string contractNo = ddlContractNo.SelectedValue.ToString();
        if (!contractNo.Equals("-1"))
        { txtContractNo.Text = contractNo; }
        else
        { txtContractNo.Text = ""; }
        ChangeBind(sender, e);
    }
    protected void ddlSoNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlSoNo.SelectedValue.Equals("-1"))
        { txtSoNo.Text = ddlSoNo.SelectedItem.Text; }
        else
        { txtSoNo.Text = ""; }
        ChangeBind(sender, e);
    }
    protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlCustomer.SelectedValue.Equals("-1"))
        { txtCustomer.Text = ddlCustomer.SelectedItem.Text; }
        else
        { txtCustomer.Text = ""; }
        ChangeBind(sender, e);
    }
    protected void ddlSaleName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlSaleName.SelectedValue.Equals("-1"))
        { txtSaleName.Text = ddlSaleName.SelectedItem.Text; }
        else { txtSaleName.Text = ""; }
        ChangeBind(sender, e);
    }
    protected void ddlContractNoCustomer_TextChanged(object sender, EventArgs e)
    {
        if (!ddlContractNoCustomer.SelectedValue.Equals("-1"))
        { txtSaleName.Text = ddlContractNoCustomer.SelectedItem.Text; }
        else { txtContractNoCustomer.Text = ""; }
        ChangeBind(sender, e);
    }
}